**Importing Excel File 
import excel "/Users/lewiskrashnsky/Documents/Princeton/Chris Referendum Project/Data/PEI/PEI 2019 Referendum and Election.xlsx", sheet("Sheet1") firstrow


**Creating New Variables 
*Total Election Vote
egen TotalVote_2019Election = rowtotal(LiberalVote PCVote GreenPartyVote NDPVote IndependentVote)

*Code for ED + Polling Number 
gen EDPOLL_Code = ElectoralDistrict + PollNumber

*Percentage Referendum Vote 
gen Yes_Ref_Percent = RefYesVote/RefTotalVote

*Percentage Party Votes 
gen Liberal_Percent_Vote = LiberalVote/TotalVote_2019Election

gen PC_Percent_Vote = PCVote/TotalVote_2019Election 

gen Green_Percent_Vote = GreenPartyVote/TotalVote_2019Election

gen NDP_Percent_Vote = NDPVote/TotalVote_2019Election

gen Major_Party_Percent_Vote = Liberal_Percent_Vote + PC_Percent_Vote

egen Minor_Party_Percent_Vote = rowtotal(Green_Percent_Vote NDP_Percent_Vote)


***Cleaning Data 
*Checking weird totals between ref and concurrent election. Nothing out of the ordinary. (Maybe Charlottetown - Hillsborough ParkA. About 101 voters who voted in the ref did not participate in election but this is a large vote total) 
gen weird_total = TotalVote_2019Election - RefTotalVote

*Checking if referendum total provided is accurate number: result is no bad observations
egen Created_ref_total = rowtotal(RefYesVote RefNoVote)
gen weird_total_2 = RefTotalVote - Created_ref_total


***Analysis***

**Scatter Plots**
graph twoway (lfit Yes_Ref_Percent Liberal_Percent_Vote) (scatter Yes_Ref_Percent Liberal_Percent_Vote)

graph twoway (lfit Yes_Ref_Percent PC_Percent_Vote) (scatter Yes_Ref_Percent PC_Percent_Vote)

graph twoway (lfit Yes_Ref_Percent Green_Percent_Vote) (scatter Yes_Ref_Percent Green_Percent_Vote)

graph twoway (lfit Yes_Ref_Percent NDP_Percent_Vote) (scatter Yes_Ref_Percent NDP_Percent_Vote)

*Combined Party Votes (PC + Libs = Major ; Greens + NDP = Minor)
graph twoway (lfit Yes_Ref_Percent Major_Party_Percent_Vote) (scatter Yes_Ref_Percent Major_Party_Percent_Vote)

graph twoway (lfit Yes_Ref_Percent Minor_Party_Percent_Vote) (scatter Yes_Ref_Percent Minor_Party_Percent_Vote)


**Regression Models** 
regress Yes_Ref_Percent Major_Party_Percent_Vote, robust
outreg2 using PEI_Major_Party_2019.doc, replace ctitle (2019 Referendum Yes Percentage (OLS))

regress Yes_Ref_Percent Minor_Party_Percent_Vote, robust 
outreg2 using PEI_Minor_Party_2019.doc, replace ctitle (2019 Referendum Yes Percentage (OLS))


**Lowess Figures** 
lowess Yes_Ref_Percent Major_Party_Percent_Vote

lowess Yes_Ref_Percent Minor_Party_Percent_Vote


**Trying normal transformation of data** 
gen normal_major_percent_vote = invnormal(Major_Party_Percent_Vote)

gen normal_minor_percent_vote = invnormal(Minor_Party_Percent_Vote)

gen normal_yes_ref_vote = invnormal(Yes_Ref_Percent )

*Figures of normal transformations of data 
graph twoway (lfit normal_yes_ref_vote normal_major_percent_vote) (scatter normal_yes_ref_vote normal_major_percent_vote)

lowess normal_yes_ref_vote normal_major_percent_vote

graph twoway (lfit normal_yes_ref_vote normal_minor_percent_vote) (scatter normal_yes_ref_vote normal_minor_percent_vote)

lowess normal_yes_ref_vote normal_minor_percent_vote

***Multivariate Regression models
regress Yes_Ref_Percent Major_Party_Percent_Vote, robust
regress Yes_Ref_Percent Minor_Party_Percent_Vote, robust

regress Yes_Ref_Percent Major_Party_Percent_Vote Minor_Party_Percent_Vote, robust
outreg2 using Multivariate_2019_PEI_Models.doc, replace ctitle (Percentage Support for Electoral Reform, 2019 PEI Referendum (OLS))

regress Yes_Ref_Percent Minor_Party_Percent_Vote, robust
outreg2 using Minor_Party_2019_PEI_Model.doc, replace ctitle (Percentage Support for Electoral Reform, 2019 PEI Referendum (OLS))


**Final Models for Supplemental Material**
regress Yes_Ref_Percent Major_Party_Percent_Vote, robust
outreg2 using Major_Party_2019_PEI_Model.doc, replace ctitle (Percentage Support for Electoral Reform, 2019 PEI Referendum (OLS))

regress Yes_Ref_Percent Major_Party_Percent_Vote [pweight = TotalVote_2019Election], robust
outreg2 using Supplementary_Regression_2019_PEI_Weighted.doc, replace ctitle (Percentage Support for Electoral Reform, 2019 PEI Referendum (OLS))


***Check observations with low ref total 
gen voting_area_low_ref_support = .
replace voting_area_low_ref_support = 1 if Yes_Ref_Percent<.3


***Merging in 2023 PEI Election Data 
merge m:m ElectoralDistrict PollNumber EDPOLL_Code using "/Users/lewis.krashinsky/Documents/Princeton/Chris Referendum Project/Data/PEI/2023 Election Results/PEI 2023 Election.dta"



**Figures for 2023 Election and 2019 referendum results 
graph twoway (lfit Yes_Ref_Percent Major_Party_Percent_Vote_2023) (scatter Yes_Ref_Percent Major_Party_Percent_Vote_2023)

graph save "Graph" "/Users/lewis.krashinsky/Documents/Princeton/Chris ReferendumProject/Data/PEI/2023 Election and 2019 Referendum Plot.gph"
file /Users/lewis.krashinsky/Documents/Princeton/Chris Referendum Project/Data/PEI/2023 Election and 2019 Referendum Plot.gph saved


**Lib vs. Major Party Vote Correlation
regress Yes_Ref_Percent Liberal_Percent_Vote, robust

regress Yes_Ref_Percent Major_Party_Percent_Vote, robust




